package com.lhb.components;

import com.lhb.common.constant.common;
import com.lhb.service.LoginJFrameService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.swing.*;
import java.awt.*;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

@Component
public class LoginJFrame extends JFrame {



    private JLabel avatar;//中间图标
    private JLabel Username,Password;//用户，密码图标
    private JTextField Username_Text;//用户框
    private JPasswordField Password_Field;//密码框
    private JButton Login,Reset;//登录按钮,重置密码按钮

    @Autowired
    private LoginJFrameService loginJFrameService;
    @Autowired
    private MainJFrame mainJFrame;

    public  LoginJFrame createLoginJFrame()
    {
        LoginJFrame loginJFrame = new LoginJFrame();
        loginJFrame.setResizable(false);
        loginJFrame.setIconImage(new ImageIcon("src/main/resources/image/logo.png").getImage());
        loginJFrame.setTitle("学生信息管理系统");
        loginJFrame.setLayout(null);
        loginJFrame.setSize(700,700);
        loginJFrame.setLocationRelativeTo(null);
        loginJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        loginJFrame.getContentPane().setBackground( new Color(255, 255, 255));
        ImageIcon avatarIcon = new ImageIcon("src/main/resources/image/avatar.png");
        avatarIcon.setImage(avatarIcon.getImage().getScaledInstance(100, 100, Image.SCALE_SMOOTH));
        avatar=new JLabel(avatarIcon);

        ImageIcon UsernameIcon = new ImageIcon("src/main/resources/image/username.png");
        UsernameIcon.setImage(UsernameIcon.getImage().getScaledInstance(50, 50, Image.SCALE_SMOOTH));
        Username= new JLabel(UsernameIcon);

        ImageIcon PasswordIcon = new ImageIcon("src/main/resources/image/password.png");
        PasswordIcon.setImage(PasswordIcon.getImage().getScaledInstance(50, 50, Image.SCALE_SMOOTH));
        Password = new JLabel(PasswordIcon);

        Username_Text = new JTextField(common.USERNAME_PROMPT);
        Username_Text.setBackground(Color.WHITE);
        Password_Field  =  new JPasswordField(common.PASSWORD_PROMPT);
        Password_Field.setBackground(Color.WHITE);



        Username_Text.addFocusListener(new FocusAdapter() {
            @Override
            public void focusGained(FocusEvent e) {
                if (Username_Text.getText().equals(common.USERNAME_PROMPT))
                {
                    Username_Text.setText("");
                    Username_Text.setBackground(Color.cyan);
                }
            }

            @Override
            public void focusLost(FocusEvent e) {

                if (Username_Text.getText().equals(""))
                {
                    Username_Text.setText(common.USERNAME_PROMPT);
                    Username_Text.setBackground(Color.white);
                }


            }
        });

        Password_Field.addFocusListener(new FocusAdapter() {
            @Override
            public void focusGained(FocusEvent e) {
                if (Password_Field.getText().equals(common.PASSWORD_PROMPT))
                {
                    Password_Field.setText("");
                    Password_Field.setBackground(Color.CYAN);
                }
            }

            @Override
            public void focusLost(FocusEvent e) {

                if (Password_Field.getText().equals(""))
                {
                    Password_Field.setText(common.PASSWORD_PROMPT);
                    Password_Field.setBackground(Color.white);
                }


            }
        });



        avatar.setBounds(280,50,100,100);
        Username.setBounds(150,200,50,50);
        Password.setBounds(150,300,50,50);

        Username_Text.setBounds(250,200,150,30);
        Password_Field.setBounds(250,300,150,30);


        loginJFrame.add(avatar);
        loginJFrame.add(Username);
        loginJFrame.add(Password);
        loginJFrame.add(Username_Text);
        loginJFrame.add(Password_Field);


        Login= new JButton("登录");
        Login.setBounds(250,380,150,40);
        Login.setFont(new Font("华文仿宋",Font.BOLD,15));
        Login.setFocusPainted(false);//去除焦点边框
        Login.setBorder(null);//去除最外边边框
        Login.setBackground(Color.WHITE);
        Login.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));

        Login.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                boolean flag = loginJFrameService.login(Username_Text.getText(), Password_Field.getText());

                if (flag)
                {
                    mainJFrame.createMainJFrame();
                    loginJFrame.setVisible(false);
                }
                else {

                    JOptionPane.showMessageDialog(loginJFrame,"用户名或密码错误","",JOptionPane.WARNING_MESSAGE);
                }
            }

            @Override
            public void mouseEntered(MouseEvent e) {
                Login.setBackground(Color.cyan);

            }

            @Override
            public void mouseExited(MouseEvent e) {
                Login.setBackground(Color.white);

            }
        });





        loginJFrame.add(Login);


        Reset =new JButton("重置密码");
        Reset.setFont(new Font("宋体",Font.BOLD,20));
        Reset.setBorder(null);//去除最外层边框
        Reset.setContentAreaFilled(false);//去除按钮点击填充
        Reset.setFocusPainted(false);//去除焦点边框
        Reset.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));//手形状光标
        Reset.setBackground(Color.WHITE);
        Reset.setForeground(Color.CYAN);
        Reset.setBounds(580,620,100,30);

        Reset.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {

                String username = JOptionPane.showInputDialog("请输入你要重置的用户名");
                JOptionPane.showConfirmDialog(loginJFrame,"是否将用户"+username+"的姓名改为"+common.PASSWORD_RESET);

                loginJFrameService.resetPassword(username);


            }

        });




        loginJFrame.add(Reset);


        loginJFrame.setVisible(true);
        return loginJFrame;
    }




    public JButton getLogin() {
        return Login;
    }

    public void setLogin(JButton login) {
        Login = login;
    }

    public JPasswordField getPassword_Field() {
        return Password_Field;
    }

    public void setPassword_Field(JPasswordField password_Field) {
        Password_Field = password_Field;
    }

    public JTextField getUsername_Text() {
        return Username_Text;
    }

    public void setUsername_Text(JTextField username_Text) {
        Username_Text = username_Text;
    }

    public JLabel getPassword() {
        return Password;
    }

    public void setPassword(JLabel password) {
        Password = password;
    }

    public JLabel getUsername() {
        return Username;
    }

    public void setUsername(JLabel username) {
        Username = username;
    }

    public JLabel getAvatar() {
        return avatar;
    }

    public void setAvatar(JLabel avatar) {
        this.avatar = avatar;
    }

    @Override
    public String toString() {
        return "LoginJFrame{" +
                "avatar=" + avatar +
                ", Username=" + Username +
                ", Password=" + Password +
                ", Username_Text=" + Username_Text +
                ", Password_Field=" + Password_Field +
                ", Login=" + Login +
                '}';
    }
}
